package com.zerone.pay.trade.web.controller.weixin;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * Created by fire on 2017/9/7.
 */
@RestController
public class WeixinTokenController  {


	protected Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private WechatProcessProvider processProvider;

	@RequestMapping(value = "/aerairble", method = { RequestMethod.GET, RequestMethod.POST })
	public String home(HttpServletRequest request, HttpServletResponse response) {

		logRequest(request);
		Map requestParamMap = request.getParameterMap();
		if (requestParamMap.containsKey("signature")&& requestParamMap.containsKey("nonce")
			&& requestParamMap.containsKey("echostr") && requestParamMap.containsKey("timestamp")) {
			return request.getParameter("echostr");
		}
		String result = processProvider.execute(request);

		logger.info("result--[{}]", result);
		return result;
	}

	private void logRequest(HttpServletRequest request) {
		if (!request.getParameterMap().isEmpty()) {
			request.getParameterMap()
					.forEach((key, value) -> logger.info("request--key:[{}],value:[{}]", key, value));
		}
	}
}
